﻿using System;
using FileSecurityManager.BizEntities;
using FileSecurityManager.BizLogic.Exceptions;
using FileSecurityManager.DataAccess.Exceptions;
using FileSecurityManager.DataAccess.Repositories;

namespace FileSecurityManager.BizLogic
{
    public class LoginLogic : BaseLogic
    {
        private static UserModel currentUser;

        /// <summary>
        /// Gets the current user.
        /// </summary>
        /// <value>The current user.</value>
        public static UserModel CurrentUser
        {
            get { return currentUser; }
        }

        #region public methods

        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="userPassword">The user password.</param>
        /// <returns>If user valid or no</returns>
        public bool ValidateUser(string userName, string userPassword)
        {
            try
            {
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userPassword))
                {
                    return false;
                }

                var isUserValid = false;

                using (var repository = new UserLinq2SqlRepository())
                {
                    var user = repository.GetByName(userName);
                    if (user != null)
                    {
                        if (user.Password == userPassword)
                        {
                            isUserValid = true;
                        }
                    }
                }

                Log.Info(string.Format("ValidateUser({0}) returned {1}", userName, isUserValid));
                return isUserValid;
            }
            catch (DataAccessException ex)
            {
                var msg = string.Format("Validate User failed because of database error: {0}", ex.Message);
                Log.Info(msg, ex);
                throw new BizLogicException(msg, ex);
            }
            catch (Exception ex)
            {
                var msg = string.Format("Validate User failed: {0}", ex.Message);
                Log.Info(msg, ex);
                throw new BizLogicException(msg, ex);
            }
        }

        /// <summary>
        /// Authorizes the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        public void AuthorizeUser(string userName)
        {
            try
            {
                if (string.IsNullOrEmpty(userName))
                {
                    return;
                }

                using (var repository = new UserLinq2SqlRepository())
                {
                    var user = repository.GetByName(userName);
                    if (user != null)
                    {
                        currentUser = user;
                    }
                }
            }
            catch (DataAccessException ex)
            {
                var msg = string.Format("Authorize User failed because of database error: {0}", ex.Message);
                Log.Info(msg, ex);
                throw new BizLogicException(msg, ex);
            }
            catch (Exception ex)
            {
                var msg = string.Format("Authorize User failed: {0}", ex.Message);
                Log.Info(msg, ex);
                throw new BizLogicException(msg, ex);
            }
        }
        #endregion
    }
}
